package com.service.bracelet.common;

import com.service.bracelet.util.UtilConsts;
import org.apache.commons.collections4.MapUtils;

import java.util.Map;

/**
 * 分页返回结果
 * @param <T>
 */
public class CommonResultPage<T> extends CommonResult {
    private int pageNumber;
    private int pageSize;
    private long total;

    public CommonResultPage(Map<String, Object> params) {
        this.pageNumber = MapUtils.getInteger(params, UtilConsts.PARAMS_PAGENUMBER, 1);
        this.pageSize = MapUtils.getInteger(params, UtilConsts.PARAMS_PAGESIZE, UtilConsts.DEFALUT_PAGESIZE);
        this.total = MapUtils.getLong(params, UtilConsts.PARAMS_TOTAL, -1l);
    }

    public CommonResultPage setTotal(long total) {
        this.total = total;
        return this;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public int getPageSize() {
        return pageSize;
    }

    public long getTotal() {
        return total;
    }

    public boolean existTotal() {
        return this.total != -1;
    }

    public static void setPageParams(Map<String, Object> params) {
        int pageNumber = MapUtils.getInteger(params, UtilConsts.PARAMS_PAGENUMBER, 1);
        int pageSize = MapUtils.getInteger(params, UtilConsts.PARAMS_PAGESIZE, UtilConsts.DEFALUT_PAGESIZE);
        params.put(UtilConsts.PARAMS_START, (pageNumber - 1) * pageSize);
        params.put(UtilConsts.PARAMS_SIZE, pageSize);
    }
}
